package com.wanaofu.managedbeans;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;

import com.wanaofu.model.ProductInfo;
import com.wanaofu.service.ProductService;
import com.wanaofu.utils.FacesUtils;

/**
 * @author jianguo.shi
 * 
 */
@ManagedBean(name = "productControlBean")
@RequestScoped
public class ProductControlBean implements java.io.Serializable {
	private static final long serialVersionUID = 6866250699219535733L;

	@ManagedProperty(name = "productService", value = "#{productService}")
	private ProductService productService;

	private List<ProductInfo> products = new ArrayList<ProductInfo>();
	private ProductInfo selectedRow;
    private String isSeletedRow ; 
    
    @ManagedProperty(value="#{param.opCode}")
    private String opCode;
    
	public ProductControlBean() {
		System.out.println("===========in ProductControlBean Constructor===" );
	}

	@PostConstruct
	public void initProductBean() {
		
//		this.opCode = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("opCode") ;
		
        System.out.println("===========in initProductBean===============opCode:"  + this.opCode );
        
		this.opCode = FacesUtils.getRequestParameter("opCode") ;
		
		
		System.out.println("===========in initProductBean===============opCode:"  + this.opCode );
		
		this.products = productService.getAllProducts(); 
        
		if (opCode != null && opCode.length() > 0) {
			selectedRow = productService.getProductInfoById(this.opCode) ;
			isSeletedRow = String.valueOf(true);
		} 		
	}

	public ProductInfo getSelectedRow() {
		return selectedRow;
	}

	public void setSelectedRow(ProductInfo selectedRow) {
		this.selectedRow = selectedRow;
	}

//	public void checkSelectedRow() {
//		RequestContext rq = RequestContext.getCurrentInstance();
//		rq.addCallbackParam("isSeletedRow", (selectedRow != null));
//	}
	
	public void onRowSelected(SelectEvent event) {
		
		System.out.println("=============onRowSelected==============");
		
		ProductInfo book = (ProductInfo) event.getObject();
	    
		FacesUtils.setMessage("Hello " + book.getCode());
//		FacesContext context = FacesContext.getCurrentInstance();  
//        context.addMessage(null, new FacesMessage("Successful", "Hello " + book.getCode()));  
        isSeletedRow = String.valueOf(true) ;
	}
	
	public String toMaintainProduct() {
		System.out.println("=============toMaintainProduct==============" ) ;
		if (selectedRow == null) {
			System.out.println("=============toMaintainProduct=========this.selectedRow is  null=====" ) ;
//			RequestContext context = RequestContext.getCurrentInstance();  
	        //execute javascript oncomplete
			//RequestContext.getCurrentInstance().execute("alertDialog.show();") ;
			RequestContext.getCurrentInstance().execute("checkSelectedRow();") ;
			
	        return null;
		}
		
	//	FacesContext.getCurrentInstance().getExternalContext().getFlash().put("opCode",this.selectedRow.getId());
		System.out.println("=============toMaintainProduct============" +this.selectedRow.getId() ) ;
		return "product_maintain?faces-redirect=true&amp;opCode=" + this.selectedRow.getId(); 
//		this.opCode = this.selectedRow.getId() ;
//		return "product_maintain";
	}
	

	
	public List<ProductInfo> getProducts() {
		return products;
	}

	public void setProductService(ProductService userService) {
		this.productService = userService;
	}

	public String getIsSeletedRow() {
		return isSeletedRow;
	}

	public void setIsSeletedRow(String isSeletedRow) {
		this.isSeletedRow = isSeletedRow;
	}

	public String getOpCode() {
		return opCode;
	}

	public void setOpCode(String opCode) {
		this.opCode = opCode;
	}

	/*
	 * public String getName() { // 1 得到应用程序作用域 //
	 * FacesContext.getCurrentInstance
	 * ().getExternalContext().getApplicationMap() // 2 得到session作用域 //
	 * FacesContext.getCurrentInstance().getExternalContext().getSessionMap() //
	 * 3 得到request作用域 //
	 * FacesContext.getCurrentInstance().getExternalContext().getRequestMap() //
	 * 4 得到请求参数的Map //
	 * FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap
	 * ()
	 * 
	 * Map<String,Object> sessionMap =
	 * FacesContext.getCurrentInstance().getExternalContext().getSessionMap() ;
	 * 
	 * Set<String> keySet = sessionMap.keySet() ; for(String key : keySet) {
	 * System.out.println( "====key in session:" + key + "==value:" +
	 * sessionMap.get(key)+ "==="); }
	 * 
	 * System.out.println("====session languange:" +
	 * FacesContext.getCurrentInstance
	 * ().getViewRoot().getLocale().getDisplayLanguage());
	 * System.out.println("====session country:" +
	 * FacesContext.getCurrentInstance
	 * ().getViewRoot().getLocale().getDisplayCountry());
	 * 
	 * this.name = productService.getProductInfoByCode("P001").getShortName() ;
	 * return name; }
	 */
}